import os,time
import requests
from urllib.parse import urlencode
from urllib.request import urlretrieve

def getPage(offset):
    '''爬取指定url页面信息'''
    params = {
        'ipn': 'rjt',
        'tn': 'resultjson_com',
        'ct': '201326592',
        'queryWord': '街拍',
        'word': '街拍',
        'autoload': 'true',
        'pn': offset,
        'rn': '30',
        'from': 'gallery',
    }
    url = 'https://image.baidu.com/search/acjson?' + urlencode(params)
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
    except requests.ConnectionError:
        return None

def getImages(json, n):
    '''解析获取图片信息'''
    data = json.get('data')
    if data:
        for item in data:
            n += 1
            yield {
                'index': n,
                'image': item.get('thumbURL'),
            }

def saveImage(item):
    '''储存图片'''
    path = "./baidupic/"
    if not os.path.exists(path):
        os.mkdir(path)
    # 判断是否imageurl是否存在
    if item.get('image'):
        # 使用urllib中urlretrieve直接存储图片
        urlretrieve(item.get('image'), './baidupic/'+ str(item.get('index')) + '.jpg')

def main(offset):
    ''' 主程序函数，负责调度执行爬虫处理 '''
    json = getPage(offset)
    for item in getImages(json, offset):
        print(item)
        saveImage(item)

# 判断当前执行是否为主程序运行，并遍历调用主函数爬取数据
if __name__ == '__main__':
    # main(0)
    for i in range(3):
        main(offset=i*30)
        time.sleep(1)